﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Imports System.IO
Imports Boogaloo.Core.UI
Imports Boogaloo.Admin.UI.Controls
Imports Boogaloo.Core
Imports Boogaloo.Core.BLL


Namespace UI.Webpages


    Partial Public Class Themes
        Inherits BaseAdminPage

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            If Not Page.IsPostBack Then
                lit_success.Visible = False

                Dim dirs As String() = Directory.GetDirectories(Server.MapPath("~/App_Themes/"))
                Dim dir As String = String.Empty
                For Each dir In dirs
                    Dim dirInfo As New DirectoryInfo(dir)
                    If Not ((CDbl(dirInfo.Attributes And FileAttributes.Hidden)) = FileAttributes.Hidden) Then chkl_themes.Items.Add(New ListItem(dir.Replace(Server.MapPath("~/App_Themes/"), "")))
                Next

                Dim themes As List(Of Theme) = Groove.admin_root.themes
                If themes.Count = 0 Then
                    up_themeconfig.Visible = False
                Else
                    up_themeconfig.Visible = True
                    ddl_theme_config.DataSource = themes
                    ddl_theme_config.DataValueField = "theme_id"
                    ddl_theme_config.DataTextField = "name"
                    ddl_theme_config.DataBind()
                End If
                

                If chkl_themes.Items.Count = 0 Then
                    Me.Visible = False
                Else
                    BindControl()
                End If

            End If

        End Sub

        Protected Sub BindControl()
            pnl_delete.Visible = False
            Dim item As ListItem = Nothing
            Dim current As Dictionary(Of String, Theme) = BLL.Theme.GetThemesDictionary(Groove.admin_root.root_id)
            For Each item In chkl_themes.Items
                If current.ContainsKey(item.Text) Then item.Selected = True
            Next


            With lhddl_globaltheme
                .dropdownlist.DataSource = Groove.admin_root.themes
                .dropdownlist.DataTextField = "name"
                .dropdownlist.DataValueField = "theme_id"
                .dropdownlist.DataBind()
                .dropdownlist.Items.Insert(0, New ListItem("ASP.NET Themes not used", 0))


                Dim assigned_themes As List(Of Theme) = Core.BLL.Theme.GetCurrentNodeThemes(Groove.admin_root.root_id)
                If assigned_themes.Count >= 1 Then
                    If Core.BLL.Theme.AllNodesInRootHaveTheme(Groove.admin_root.root_id, assigned_themes(0).theme_id) Then

                        Try
                            .dropdownlist.SelectedValue = assigned_themes(0).theme_id.ToString
                        Catch ex As Exception
                            .label = "(Click to globally set the theme for this host)"
                        End Try

                        .label = String.Format("All pages within this host are currently using the '{0}' ASP.NET Theme", assigned_themes(0).name)
                    Else
                        .dropdownlist.Items.Add(New ListItem("Mixed ASP.NET Themes", -1))
                        .dropdownlist.SelectedValue = -1
                        .label = "This host uses more than one ASP.NET Theme"
                    End If
                Else
                    .label = "(Click to globally set the theme for this host)"
                End If

            End With


            ' Theme configuration
            If up_themeconfig.Visible Then

                Dim th As BLL.Theme = BLL.Theme.GetTheme(CType(ddl_theme_config.SelectedValue, Integer))
                hyp_stylesheeteditor.NavigateUrl = String.Format("{0}StyleSheetEditor.aspx?theme={1}", Groove.path_admin_host, th.theme_id.ToString)

                Dim manifest As ThemeCSSManifest = th.manifest
                If manifest IsNot Nothing Then

                    chk_manifest_bundle.Checked = manifest.bundle
                    chk_manifest_compress.Checked = manifest.compress
                    chk_manifest_removewhitespace.Checked = manifest.removewhitespace

                    rpt_theme_conditions.DataSource = manifest.stylesheets
                    rpt_theme_conditions.DataBind()
                End If

            End If

        End Sub

#Region "Theme selection"


        Protected Sub chkl_themes_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chkl_themes.SelectedIndexChanged

            Dim chkl As CheckBoxList = CType(sender, CheckBoxList)
            Dim exist As Dictionary(Of String, Theme) = BLL.Theme.GetThemesDictionary(Groove.admin_root.root_id)

            ' figure out if this is an add or delete
            Dim item As ListItem = Nothing
            For Each item In chkl.Items
                Dim found As Boolean = False
                If Not exist.ContainsKey(item.Text) And item.Selected Then
                    ' this is a new item
                    BLL.Theme.InsertTheme(item.Text, Groove.admin_root.root_id)
                    found = True
                    lit_success.Visible = True
                    Groove.RefreshPage()
                ElseIf exist.ContainsKey(item.Text) And Not item.Selected Then
                    ' this is a delete 
                    pnl_delete.Visible = True
                    lb_delete.CommandArgument = exist(item.Text).theme_id
                    hyp_cancel.NavigateUrl = Request.Url.AbsoluteUri
                    chkl_themes.Visible = False
                    found = True
                End If
                If found Then Exit For
            Next


        End Sub

        Protected Sub lb_delete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_delete.Click

            Dim lb As LinkButton = CType(sender, LinkButton)
            Dim theme_id As Integer = CType(lb.CommandArgument, Integer)

            Dim success As Boolean = BLL.Theme.DeleteTheme(theme_id)

            Groove.RefreshPage()

        End Sub


        Protected Sub lhddl_globaltheme_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lhddl_globaltheme.Click
            With lhddl_globaltheme.dropdownlist
                If CType(.SelectedValue, Integer) >= 0 Then Groove.admin_root.UpdateAllRootNodesTheme(CType(.SelectedValue, Integer))
            End With
            BindControl()
        End Sub

#End Region


#Region "Theme configuration"

        Protected Sub ddl_theme_config_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddl_theme_config.SelectedIndexChanged
            BindControl()
        End Sub


        Protected Sub rpt_theme_conditions_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_theme_conditions.ItemDataBound

            Dim condition As KeyValuePair(Of String, List(Of ThemeCSSStyleSheet)) = CType(e.Item.DataItem, KeyValuePair(Of String, List(Of ThemeCSSStyleSheet)))

            If condition.Key IsNot Nothing Then


                Dim lbl_condition As Label = CType(e.Item.FindControl("lbl_condition"), Label)
                lbl_condition.Text = condition.Key

                Dim rpt As Repeater = CType(e.Item.FindControl("rpt_stylesheets"), Repeater)
                If rpt IsNot Nothing Then

                    rpt.DataSource = condition.Value
                    rpt.DataBind()

                End If


            End If

        End Sub


        Protected Sub rpt_stylesheets_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)

            Dim css As ThemeCSSStyleSheet = CType(e.Item.DataItem, ThemeCSSStyleSheet)
            If css IsNot Nothing Then

                CType(e.Item.FindControl("lbl_css"), Label).Text = css.filename

                Dim lhddl As LabelHiddenDropDownBox = CType(e.Item.FindControl("lhddl_moveto_group"), LabelHiddenDropDownBox)
                lhddl.CommandArgument = css.path
                If lhddl IsNot Nothing Then
                    With lhddl.dropdownlist.Items
                        .Clear()
                        .Add(New ListItem("none"))
                        .Add(New ListItem("[if IE]"))
                        .Add(New ListItem("[if lt IE 8]"))
                        .Add(New ListItem("[if lt IE 7]"))
                        .Add(New ListItem("[if gt IE 8]"))
                        .Add(New ListItem("[if gt IE 7]"))
                        .Add(New ListItem("[if gt IE 6]"))
                        .Add(New ListItem("Don't display", "nodisplay"))
                    End With
                End If

                Dim lht As LabelHiddenTextbox = CType(e.Item.FindControl("lht_media"), LabelHiddenTextbox)
                If Not String.IsNullOrEmpty(css.media) Then
                    lht.label = String.Format("Media setting is: {0}", css.media)
                    lht.Text = css.media
                End If
                lht.CommandArgument = css.path

            End If

        End Sub

        Protected Sub lht_media_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim lht As LabelHiddenTextbox = CType(sender, LabelHiddenTextbox)
            If lht IsNot Nothing Then
                Dim csspath As String = lht.CommandArgument

                Dim th As BLL.Theme = BLL.Theme.GetTheme(CType(ddl_theme_config.SelectedValue, Integer))
                Dim success As Boolean = th.manifest.UpdateStyleSheetMedia(csspath, lht.Text)
                BindControl()
            End If
        End Sub


        Protected Sub lhddl_moveto_group_Click(ByVal sender As Object, ByVal e As EventArgs)

            Dim lhddl As LabelHiddenDropDownBox = CType(sender, LabelHiddenDropDownBox)
            If lhddl IsNot Nothing Then
                Dim csspath As String = lhddl.CommandArgument

                Dim th As BLL.Theme = BLL.Theme.GetTheme(CType(ddl_theme_config.SelectedValue, Integer))
                Dim success As Boolean = th.manifest.SetStyleSheetGroup(csspath, lhddl.dropdownlist.SelectedValue)
                BindControl()

            End If

        End Sub


        Protected Sub chk_manifest_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) _
            Handles chk_manifest_bundle.CheckedChanged, chk_manifest_compress.CheckedChanged, chk_manifest_removewhitespace.CheckedChanged

            Dim manifest As ThemeCSSManifest = BLL.Theme.GetTheme(CType(ddl_theme_config.SelectedValue, Integer)).manifest

            If manifest IsNot Nothing Then

                manifest.bundle = chk_manifest_bundle.Checked
                manifest.compress = chk_manifest_compress.Checked
                manifest.removewhitespace = chk_manifest_removewhitespace.Checked

                manifest.SaveManifest()

                BindControl()
            End If


        End Sub

#End Region

    End Class

End Namespace

